<template>
	<view>
		<!-- 还要记得这里加上组件 -->
		<scan></scan>
		<uni-card title="采购单信息" extra="">
			<text> 货品编码：{{goodsNumStr}}</text></br>
			<text> 采购单号：{{orderData.purchaseNo}}</text></br>
			<text> 采购数量：{{orderData.goodsCount}}，已扫入数量：{{scanNum}}，到货数量：{{stockNum}}，待发货数量：{{waitNum}}</text>
		</uni-card>
		<view>
			<uni-section class="mb-10" title="箱码:">
				<template v-slot:right>
					<view style="width: 550rpx;">
						<uni-easyinput :focus="true" type="text" confirmType="next" placeholder="请扫入箱码"  
							@confirm="confirmXiangZi"></uni-easyinput>
					</view>
				</template>
			</uni-section>
		</view>
		<uni-card>
			<template v-slot:title>
				</br>
				<uni-easyinput  :focus="focus"  type="text" confirmType="next" v-model="goodsNumInput"  placeholder="请扫入商品码"
					@confirm="confirmGoods"></uni-easyinput>
			</template>
			<scroll-view :scroll-top="0" :scroll-y="true" class="scroll-Y" :show-scrollbar="true">
				<view v-for="(item,index) in goodsNum" style="border:1px dashed #000">
					<uni-section class="mb-10" :title="'商品码'+(index+1)+':'">
						<template v-slot:right>
							<view>
								<text>{{item.value}}</text>
							</view>

						</template>
					</uni-section>
					<view style="margin-left: 40%;display: flex;">
						<text>扫入数量： </text>
						 <u-number-box v-model="item.num"  min="0"></u-number-box>
					</view>
				</view>
			</scroll-view>
		</uni-card>
		<view style="text-align: center;margin-top: 3%;">
			<button plain="true" size="default" @click="show = true">提交</button>
		</view>
		<u-popup :show="show" @close="show = false" @open="open" mode="center">
			<view style="text-align: center;">
				<text>请确认</text>
			</view> 
			
		      <view>
		      	<uni-section class="mb-10" title="箱码:">
		      		<template v-slot:right>
		      			<view style="width: 550rpx;">
		      				<uni-easyinput :value="xiangValue"  :disabled="true" type="text" confirmType="next" placeholder="请扫入箱码"    ></uni-easyinput>
		      			</view>
		      		</template>
		      	</uni-section>
		      </view>
			  <uni-card>
			   
			  	<scroll-view :scroll-top="0" :scroll-y="true" class="scroll-Y" :show-scrollbar="true">
			  		<view v-for="(item,index) in goodsNum" style="border:1px dashed #000">
			  			<uni-section class="mb-10" :title="'商品码'+(index+1)+':'">
			  				<template v-slot:right>
			  					<view>
			  						<text>{{item.value}}</text>
			  					</view>
			  
			  				</template>
			  			</uni-section>
			  			<view style="margin-left: 40%;display: flex;">
			  				<text>扫入数量： </text>
			  				 <u-number-box v-model="item.num"  min="0" :disabled="true"></u-number-box>
			  			</view>
			  		</view>
			  	</scroll-view>
			  </uni-card>
			  <view style="text-align: center;margin-top: 3%;">
			  	<button plain="true" size="default" @click="save">确认提交</button>
			  </view>
		 </u-popup>
	</view>

</template>

<script>
	//记得引入组件
	import scan from "@/components/scan/scan.vue";
	import {
		factoryCodeSave,getGoodsInfoByPuNo,factoryCodeSaveCheck
	} from '../../config/api.js';
	export default {
		//引入组件
		components: {
			scan
		},

		data() {
			return {
				show: false,
				focus: false,
				tempValue: null,
				goodsNumInput: null,
				xiangValue:null,
				orderData:null,
				goodsNumStr:null,
				goodsDetail:null,
				scanNum:0,
				waitNum:0,
				stockNum:0,
				goodsNum: []
			}
		},
		onLoad(e) {
			this.orderData=JSON.parse(e.json)
			this.getGoodsInfoByPuNoFun()
		//	this.goodsNumStr=e.goodsNum
			console.log("数据：",this.orderData)
		},
		onShow() {
			let that = this
			uni.$off('scan') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scan', function(data) {
				// if(that.goodsNum.length==1){
				// 	that.goodsNum[0].value=data.code
				// }
				//扫码成功后的回调，你可以写自己的逻辑代码在这里
				console.log('扫码结果：', data.code);
			})
		},
		methods: {
			 getGoodsInfoByPuNoFun(){
				 let  that=this;
				 getGoodsInfoByPuNo({
				 	custom: {
				 		auth: true
				 	},
				 	params: {
				 		puNo: this.orderData.purchaseNo,
				 	},
				 }).then((e) => {
				 	if (e.code == 200) {
				 		that.goodsNumStr = e.data.orderDetails[0].goodsNo
				 		that.goodsDetail = e.data.orderDetails
				 		that.stockNum = e.data.stopNum
				 		that.waitNum = e.data.waitNum
						
						for (var i = 0; i < that.goodsDetail.length; i++) {
							if(that.goodsDetail[i].scanNum!=null){
								that.scanNum=that.scanNum+that.goodsDetail[i].scanNum
							}
							
						}
					 
				 	}
				 }).catch((e) => {
				 	console.log(e)
				 })
			 },
		async	confirmGoods(e) {
				this.focus = false
				let that = this;
				let flag = false; 
				let isCaiGou = false; 
					this.goodsNumInput="";
				for (var j = 0; j < this.goodsDetail.length; j++) {
					if(this.goodsDetail[j].specNo==e){
						isCaiGou=true;
					}
				}
				let param=[];
				for (var i = 0; i < this.goodsNum.length; i++) {
					if (this.goodsNum[i].value == e) {
						this.goodsNum[i].num = this.goodsNum[i].num + 1
						flag = true;
					}
				}
		 
				
				if(isCaiGou==false){
					uni.showToast({
						title:"采购单不存在该条码",icon:"none"
					})
					setTimeout(function(){
								that.focus = true
					},500)
					return;
				}
				if (flag == false) {
					this.goodsNum.push({
						value: e,
						xiangValue: this.xiangValue,
						num: 1
					})
				}
				for (var i = 0; i < this.goodsNum.length; i++) {
					param.push({
						caseCode:this.goodsNum[i].xiangValue,
						goodsCode:this.goodsNum[i].value,
						goodsNo:this.goodsNumStr,
						puNo:this.orderData.purchaseNo,
						puNum:this.orderData.goodsCount,
						caseNum:this.goodsNum[i].num,
						flag:2,
						num:this.goodsNum[i].num
					})
				}
			
				if(param.length>0){
				await	factoryCodeSaveCheck(param, { custom: { auth: true,loading:true } }, ).then((result) => {
						uni.showToast({
							title:result.message,icon:'none'
						})
						if(result.code!=200){
							for (var i = 0; i < this.goodsNum.length; i++) {
								 if (this.goodsNum[i].value == e) {
								 	this.goodsNum[i].num = this.goodsNum[i].num - 1
								 }
							}
						}
					
					}).catch(() => {
					
					})
				}
				setTimeout(function(){
							that.focus = true
				},500)
		
					console.log("a???" + that.tempValue)
				// this.goodsNum[index].value=""
				// if(flag==false&&this.goodsNum.length>1){

				// }
				// 	 this.fous=true;
			},
			confirmXiangZi(e) {
				this.xiangValue=e;
				this.focus = true;
			},
			save(){
				let param=[];
				for (var i = 0; i < this.goodsNum.length; i++) {
					param.push({
						caseCode:this.goodsNum[i].xiangValue,
						goodsCode:this.goodsNum[i].value,
						goodsNo:this.goodsNumStr,
						puNo:this.orderData.purchaseNo,
						puNum:this.orderData.goodsCount,
						caseNum:this.goodsNum[i].num,
						flag:2,
						num:this.goodsNum[i].num
					})
					
				}
				if(param.length==0){
					uni.showToast({
						title:"参数错误"
					})
					return
				}
				factoryCodeSave(param, { custom: { auth: true,loading:true } }, ).then((e) => {
					if(e.code==200){
						uni.showToast({
							title:"提交成功"
						})
						setTimeout(function(){
							uni.reLaunch({
								url:"/pages/gongchang/index"
							})
						},1000)
					}else{
						uni.showToast({
							title:e.message,icon:"none"
						})
					}
					
				}).catch(() => {
				
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: white;
	}

	.scroll-Y {
		height: 400rpx;
	}
</style>